using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

namespace Pay.Core {
	/// <summary>
	/// 枚举扩展方法类
	/// </summary>
	public static class EnumExtensions {

        public static Type AttributeType = typeof(DescriptionAttribute);

        /// <summary>
        /// 获取枚举项的Description特性的描述文字
        /// </summary>
        /// <param name="enumeration"> </param>
        /// <returns> </returns>
        public static string ToDescription(this Enum enumeration) {
			Type type = enumeration.GetType();
			MemberInfo[] members = type.GetMember(enumeration.CastTo<string>());
			if (members.Length > 0) {
				return members[0].ToDescription();
			}
			return enumeration.CastTo<string>();
		}
		/// <summary>
		/// 获取枚举名
		/// </summary>
		/// <returns>The name.</returns>
		/// <param name="value">Value.</param>
		public static string ToName(this Type enumType, int value) {
			var enumeration = (Enum)Enum.Parse(enumType, value.ToString());
			Type type = enumeration.GetType();
			return Enum.GetName(type, value);
		}
		/// <summary>
		/// 把枚举转换为键值对集合
		/// </summary>
		/// <param name="enumType">枚举类型</param>
		/// <param name="getText">获得值得文本</param>
		/// <returns>以枚举值为key，枚举文本为value的键值对集合</returns>
		public static Dictionary<Int32, String> ToDictionaryByValue(this Type enumType) {
			if (!enumType.IsEnum) {
				throw new ArgumentException("传入的参数必须是枚举类型！", "enumType");
			}
			Dictionary<Int32, String> enumDic = new Dictionary<int, string>();
			Array enumValues = Enum.GetValues(enumType);
			foreach (Enum enumValue in enumValues) {
				Int32 key = Convert.ToInt32(enumValue);
				String value = enumValue.ToDescription();
				enumDic.Add(key, value);
			}
			return enumDic;
		}
		/// <summary>
		/// 把枚举转换为键值对集合
		/// </summary>
		/// <param name="enumType">枚举类型</param>
		/// <returns>以枚举名称为key，枚举文本为value的键值对集合</returns>
		public static Dictionary<string, String> ToDictionary(this Type enumType) {
			if (!enumType.IsEnum) {
				throw new ArgumentException("传入的参数必须是枚举类型！", "enumType");
			}
			Dictionary<string, String> enumDic = new Dictionary<string, string>();
			Array enumValues = Enum.GetNames(enumType);
			foreach (string enumName in enumValues) {
				String value = ((Enum)Enum.Parse(enumType, enumName)).ToDescription();
				enumDic.Add(enumName, value);
			}
			return enumDic;
		}

        /// <summary>
        /// 根据枚举类型获得枚举中文描述以及枚举属性值
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<DropdownItem> GetDropdownItemList(this Type type)
        {
            FieldInfo[] fields = type.GetFields();
            var enumNames = type.GetEnumNames();
            List<DropdownItem> list = new List<DropdownItem>();
            for (int i = 0; i < enumNames.Length; i++)
            {
                FieldInfo item = type.GetField(enumNames[i]);
                object[] objs = item.GetCustomAttributes(AttributeType, false);
                string desription;
                if (objs != null && objs.Length != 0)
                {
                    DescriptionAttribute da = (DescriptionAttribute)objs[0];
                    if (string.IsNullOrEmpty(da.Description))
                        continue;
                    desription = da.Description;
                }
                else
                {
                    desription = item.Name;
                }
                int labelID = -1;
                try
                {
                    labelID = Convert.ToInt32(System.Enum.Parse(type, item.Name));
                }
                catch (Exception ex)
                {
                    desription = "[Unkown Error] " + item.Name + " " + ex.Message;
                }
                list.Add(new DropdownItem() { Text = desription, Value = labelID.ToString() });
            }
            return list;
        }
    }
}